package com.mmall.controller.backend;


import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICategoryService;
import com.mmall.service.IUserSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/manager/category/")
public class CategoryManagerController {

    @Autowired
    private IUserSService iUserSService;
    @Autowired
    ICategoryService iCategoryService;

    @RequestMapping(value = "add_category.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addCategory(HttpSession session,String categoryName,
                                                @RequestParam(value = "parentId",defaultValue ="0") int parentId){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆,请登陆");
        }
        //校验一下是不是管理员
        if (iUserSService.checkAdminRole(user).isSuccess()){
            //是管理员
            //增加我们处理分类的逻辑
            return iCategoryService.addCategory(categoryName,parentId);
        }else {
            return ServerResponse.createByErrorMessage("非管理员,无权限操作");
        }
    }

    @RequestMapping(value = "update_category_name.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updateCategoryName(HttpSession session,String categoryName,int categoryId){

        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆,请登陆");
        }
        //校验一下是不是管理员
        if (iUserSService.checkAdminRole(user).isSuccess()){
            return iCategoryService.updateCatrgory(categoryName,categoryId);
        }else {
            return ServerResponse.createByErrorMessage("非管理员,无权限操作");
        }
    }

    @RequestMapping(value = "get_category.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(HttpSession session,@RequestParam(value = "categoryId"
            ,defaultValue = "0") int categoryId){

        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆,请登陆");
        }
        //校验一下是不是管理员
        if (iUserSService.checkAdminRole(user).isSuccess()){
            // 查询当前节点下面的子节点并且不递归保持平级
            return iCategoryService.getChildrenParallelCategory(categoryId);
        }else {
            return ServerResponse.createByErrorMessage("非管理员,无权限操作");
        }
    }

    @RequestMapping(value = "get_deep_category.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse getCategoryDeepChildrenCategory(HttpSession session,@RequestParam(value = "categoryId"
            ,defaultValue = "0") int categoryId){

        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆,请登陆");
        }
        //校验一下是不是管理员
        if (iUserSService.checkAdminRole(user).isSuccess()){
            // 查询当前节点下面的子节点并且递归查询
            return iCategoryService.selectCategoryAndChildById(categoryId);
        }else {
            return ServerResponse.createByErrorMessage("非管理员,无权限操作");
        }
    }

}
